function load_flv(server,id,token) {
    if (flvjs.isSupported()) {
        var videoElement = document.getElementById('videoElement');
        var flvPlayer = flvjs.createPlayer({
            type: 'flv',
            isLive: true,
            url: `${server}/live/${id}.live.flv?token=${token}`
        });
        flvPlayer.attachMediaElement(videoElement);
        flvPlayer.load();
    }
}

function remove_video(any){
    document.getElementById('container').innerHTML = "<p id='centerp'>No channel available.</p>";
}

function get_list_and_load(server,cur_channel,token) {
    get_channels(data=>{
        const channels = data.data;
        if(channels.length === 0){
            console.log("No channel available.");
            remove_video();
            return;
        }
        let has = false;
        let sdb = document.getElementById("sidebar");
        if(!cur_channel in channels){
            console.log("Channel not found, change to the first one.");
            cur_channel = channels[0];
            document.getElementById('channel').innerText = cur_channel;
        }
        for(var ch in channels) {
            if (ch == cur_channel) {
                has = true;
                sdb.innerHTML += `<a href="/?channel=${ch}" class="active">${ch}</a>`;
            }else{
                sdb.innerHTML += `<a href="/?channel=${ch}">${ch}</a>`;
            }
        }
        load_flv(server,cur_channel,token)
    },remove_video,remove_video);
}